package com.google.zxing.client.j2se;

import gui.ava.html.image.generator.HtmlImageGenerator;

import java.io.ByteArrayOutputStream;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;


public class HtmlReport extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5863386447868283105L;
	JEditorPane myEditorPane = null;
	IReportEngine engine = null;
	EngineConfig config = null;

	HtmlReport() {
		myEditorPane = new JEditorPane();
		myEditorPane.setEditable(false);
		JScrollPane scrollPane = new JScrollPane(myEditorPane);
		JFrame myframe = new JFrame("BIRT Report");
		myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		myframe.getContentPane().add(scrollPane);
		myframe.setSize(600, 600);
		myframe.setVisible(true);
	}

	public void runReport() {
		try {

			IReportRunnable design = null;
			// Open the report design

			design = engine
					.openReportDesign("E:/cxrwork/xiaotuworld/barCodeReport/new_report.rptdesign");

			// Create task to run and render the report,

			IRunAndRenderTask task = engine.createRunAndRenderTask(design);
			HTMLRenderOption options = new HTMLRenderOption();
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			options.setOutputStream(bos);
			options.setOutputFormat("html");
			options.setEmbeddable(true);
			task.setRenderOption(options);
			task.run();
			task.close();
			myEditorPane.setContentType("text/html");
			myEditorPane.setText(bos.toString());
			HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
			imageGenerator.loadHtml(bos.toString());
			imageGenerator.saveAsImage("e:/design.png");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void startPlatform() {
		try {
			config = new EngineConfig();
			config.setBIRTHome("E:/platform");
			Platform.startup(config);
			IReportEngineFactory factory = (IReportEngineFactory) Platform
					.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
			engine = factory.createReportEngine(config);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void stopPlatform() {
		engine.destroy();
		Platform.shutdown();
	}

	public static void main(String[] args) {
		HtmlReport html = new HtmlReport();
		html.startPlatform();
		System.out.println("Started");
		html.runReport();
		html.stopPlatform();
		System.out.println("Finished");
	}
}
